//
//  GQHServiceView.m
//  Xanthium
//
//  Created by GuanQinghao on 11/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHServiceView.h"
#import "GQHSlideView.h"
#import "GQHHeader.h"

@implementation GQHServiceView

#pragma mark --Lifecycle
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    _qh_imageArray = @[@"service_banner_1",@"service_banner_2",@"service_banner_3"];
    _qh_iconArray = @[@"service_other",@"service_etc",@"service_insurance",@"service_gas"];
    _qh_titleArray = @[@"途途行", @"ETC", @"保险", @"加油卡"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    /// 轮播图
    CGFloat slideWidth = self.frame.size.width;
    CGFloat slideHeight = 0.4 * self.frame.size.width;
    CGFloat slideX = 0;
    CGFloat slideY = self.qh_statusBarHeight + self.qh_navigationBarHeight;
    CGRect frame = CGRectMake(slideX, slideY, slideWidth, slideHeight);
    GQHSlideView *slideView = [GQHSlideView qh_slideViewWithFrame:frame loop:YES imageArray:self.qh_imageArray];
    [self addSubview:slideView];
    
    /// 按钮
    CGFloat itemWidth = (self.frame.size.width - GQH_MARGIN_DOUBLE) / 3.0f;
    CGFloat itemHeight = 120.0f;
    
    for (NSUInteger i = 0; i < self.qh_iconArray.count; i++) {
        
        UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [itemButton setFrame:CGRectMake((i % 3) * itemWidth + GQH_MARGIN_SINGLE, (i / 3) * itemHeight + slideView.qh_bottom, itemWidth, itemHeight)];
        [itemButton setTag:(10 + i)];
        [itemButton setBackgroundColor:kWhiteColor];
        [itemButton addTarget:self action:@selector(clickServiceButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:itemButton];
        
        UIImageView *itemIconView = [[UIImageView alloc]init];
        [itemIconView setFrame:CGRectMake(0.5 * (itemWidth - 65), 25, 65, 65)];
        [itemIconView setImage:[UIImage imageNamed:self.qh_iconArray[i]]];
        [itemButton addSubview:itemIconView];
        
        UILabel *itemTitleLabel = [[UILabel alloc]init];
        [itemTitleLabel setFrame:CGRectMake(0, 100, itemWidth, 20)];
        [itemTitleLabel setFont:kSmallerFont];
        NSString *itemTitle = self.qh_titleArray[i];
        [itemTitleLabel setText:itemTitle];
        [itemTitleLabel setTextColor:kDarkBlackColor];
        [itemTitleLabel setTextAlignment:NSTextAlignmentCenter];
        [itemButton addSubview:itemTitleLabel];
    }
}

#pragma mark --EventMethod
- (IBAction)clickServiceButton:(UIButton *)sender {
    DLog(@"点击服务图标");
    
    if (self.clickServiceButtonBlock) {
        
        self.clickServiceButtonBlock(sender);
    }
}

@end
